package model

import (
	"context"
	"log"
	"time"

	"github.com/go-redis/redis/v8"

	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

var Mongo = InitMongo()
var RDB = InitRedis()

func InitMongo() *mongo.Database {
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)

	defer cancel()

	client, err := mongo.Connect(ctx, options.Client().SetAuth(options.Credential{
		Username:    "xxxvideoslover",
		Password:    "xxxvideoslover",
		PasswordSet: false,
	}).ApplyURI(
		// todo: replace your own
		"mongodb+srv://xxxvideoslover:xxxvideoslover@cluster0.5jzofcs.mongodb.net/go-im?retryWrites=true&w=majority&appName=Cluster0",
	))

	if err != nil {
		log.Println("connect mongodb failed", err)
		return nil
	}
	log.Println("connect mongodb success")
	return client.Database("go-im")
}

func InitRedis() *redis.Client {
	return redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
	})
}
